package org.apache.sad.core.framework.utils;

import java.text.SimpleDateFormat;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.text.ParseException;

import org.apache.xmlbeans.XmlCalendar;

public abstract class DateUtils {
    public final static String EVENT_DATE = "EventDate";
    public final static String NOTIFICATION_DATE = "NotificationDate";
    public final static String RECEIVED_DATE = "ReceivedDate";
    
    public final static String TIMESTAMP_FORMAT = "2009-11-03T15:09:04.201Z";    

	public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat();

    public static final String GMT_TIME_ZONE_ID = "GMT";
   
    public static final String ENI_TIMESTAMP_FORMAT = "yyyyMMdd:HHmmss";
 
    public static long getNotificationDate(TimeZone timeZone, String roundTrip) throws Exception {  
        SimpleDateFormat dateFormat = new SimpleDateFormat(ENI_TIMESTAMP_FORMAT);
        dateFormat.setTimeZone(timeZone); 
        
        //decode it
        byte[] result = Base64.decode(roundTrip);
        roundTrip = new String(result);  
        
        String time = roundTrip.substring(12, 27);        
        Date date = dateFormat.parse(time);//consider timezone
        return date.getTime();
        
    }
    
    /**
     * Converts a date to the standard EEP timestamp format
     * 
     * @param date
     * @return
     */
    public static String format(Calendar date) {
        if (date == null) {
            return null;
        } 
        
        XmlCalendar xc = new XmlCalendar(TIMESTAMP_FORMAT);
        xc.setTime(date.getTime()); 
        return xc.toString();        
    }   
    
    /**
     * Converts a date to the standard EEP timestamp format
     * 
     * @param date
     * @return
     */
    public static String format(Long time) {
        if (time == null) {
            return null;
        } 
        
        Calendar date = Calendar.getInstance();
        date.setTimeInMillis(time);
        XmlCalendar xc = new XmlCalendar(TIMESTAMP_FORMAT);
        xc.setTime(date.getTime()); 
        return xc.toString();        
    }    

	public static String calendarsToString(Calendar date, Calendar time) {

		Calendar dateTime = (Calendar)date.clone();
		dateTime.set(time.get(Calendar.YEAR), time.get(Calendar.MONTH),
				time.get(Calendar.DATE), time.get(Calendar.HOUR_OF_DAY),
				time.get(Calendar.MINUTE), time.get(Calendar.SECOND));
		dateTime.set(Calendar.MILLISECOND, time.get(Calendar.MILLISECOND));

		return DEFAULT_DATE_FORMAT.format(dateTime.getTime());
	}

	public static String dateToString(Date date) {
		return DEFAULT_DATE_FORMAT.format(date);
	}

	public static Date parseDate(String dateTime) throws ParseException {
		return DEFAULT_DATE_FORMAT.parse(dateTime);
	}
	
	
	// This method is used from BaseSoapHandler
	public static String getISO8601DateTime(Date date) {
        SimpleDateFormat ISO8601Local = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        TimeZone timeZone = TimeZone.getDefault(); //local JVM time zone
        ISO8601Local.setTimeZone(timeZone);
        DecimalFormat twoDigits = new DecimalFormat("00");

        int offset = ISO8601Local.getTimeZone().getOffset(date.getTime());
        String sign = "+";
        if (offset < 0) {
          offset = -offset;
          sign = "-";
        }
        int hours = offset / 3600000;
        int minutes = (offset - hours * 3600000) / 60000;

        String ISO8601Now = ISO8601Local.format(date) + sign +
          twoDigits.format(hours) + ":" + twoDigits.format(minutes);
        return ISO8601Now;
      }
}